package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/25.
 * 确认信息界面
 */

public class InvoiceConfirmActivity extends BaseActivity2 implements View.OnClickListener {

    public static final String INVOICE_ID = "INVOICE_ID";

    private Button btn_confirm_bill;

    private Dialog mConfirmDialog;

    private EntityBean mBean;

    private TextView tv_header_type;
    private TextView tv_invoice_type_c;
    private TextView tv_invoice_header_c;
    private TextView tv_taxpay_c;
    private TextView tv_invoice_content_c;
    private TextView tv_taxpayer_type_c;
    private TextView tv_invoice_mount_c;
    private TextView tv_open_bank_c;
    private TextView tv_bank_account_c;
    private TextView tv_address_c;
    private TextView tv_telphone_c;
    private TextView tv_addressee_c;
    private TextView tv_tel_num_c;
    private TextView tv_receive_address_c;
    private TextView tv_company_name_c;
    private LinearLayout ll_tax_pay;
    private LinearLayout ll_buy_info_c;
    private ImageView iv_invoice_confirm;
    private ImageView iv_back ;

    private String customertype;

    private int invoiceType;

    private int headerType;


    private int invoiceItem;

    private EntityBean bean;
    private View view_item1;
    private View view_item2;

    //发票流水id
    private ArrayList<String> invoiceIds;

    public static final String INVOICE_CONFIRM = "invoice_confirm";

    public static final int INVOICE_CONFIRM_ACTIVITY = 1;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_invoice_confirm;
    }

    @Override
    public void initView() {
        setToolbarVisible(false);
        btn_confirm_bill = (Button) findViewById(R.id.btn_confirm_bill);
        tv_header_type = (TextView) findViewById(R.id.tv_header_type);
        tv_invoice_type_c = (TextView) findViewById(R.id.tv_invoice_type_c);
        tv_invoice_header_c = (TextView) findViewById(R.id.tv_invoice_header_c);
        tv_taxpay_c = (TextView) findViewById(R.id.tv_taxpay_c);
        tv_invoice_content_c = (TextView) findViewById(R.id.tv_invoice_content_c);
        tv_taxpayer_type_c = (TextView) findViewById(R.id.tv_taxpayer_type_c);
        tv_invoice_mount_c = (TextView) findViewById(R.id.tv_invoice_mount_c);
        tv_open_bank_c = (TextView) findViewById(R.id.tv_open_bank_c);
        tv_bank_account_c = (TextView) findViewById(R.id.tv_bank_account_c);
        tv_address_c = (TextView) findViewById(R.id.tv_address_c);
        tv_telphone_c = (TextView) findViewById(R.id.tv_telphone_c);
        tv_company_name_c = (TextView) findViewById(R.id.tv_company_name_c);
        tv_addressee_c = (TextView) findViewById(R.id.tv_addressee_c);
        tv_tel_num_c = (TextView) findViewById(R.id.tv_tel_num_c);
        tv_receive_address_c = (TextView) findViewById(R.id.tv_receive_address_c);
        ll_tax_pay = (LinearLayout) findViewById(R.id.ll_tax_pay);
        ll_buy_info_c = (LinearLayout) findViewById(R.id.ll_buy_info_c);
        iv_invoice_confirm = (ImageView) findViewById(R.id.iv_invoice_confirm);
        view_item1 = findViewById(R.id.view_item1);
        view_item2 = findViewById(R.id.view_item2);
        iv_back = (ImageView) findViewById(R.id.iv_back);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_confirm_bill.setOnClickListener(this);
        initData();
        setData2View();
    }


    public void initData(){
        Intent intent = getIntent();
        String s = intent.getStringExtra(RecipientInfoActivity.RECIPIENT_INFO);
        invoiceIds = intent.getStringArrayListExtra(INVOICE_ID);
        PrintLog.e("InvoiceConfirm","invoiceIds111=:"+invoiceIds.toString());
        bean = (EntityBean) JSONSerializer.getInstance().DeSerialize(s, EntityBean.class);
        this.mBean = (EntityBean) bean.get("invoicebean");
    }

    public void setData2View(){
        customertype = mBean.get("customertype").toString();  //抬头类型
        String invtype = mBean.get("invtype").toString();            //发票类型
        String customername = mBean.get("customername").toString();  //发票抬头
        String invoiceitem = mBean.get("invoiceitem").toString();
        String taxpayertype = mBean.get("taxpayertype").toString();
        String amt = mBean.get("amt").toString();
        String addressee = mBean.get("addressee").toString();
        String mobile = mBean.get("mobile").toString();
        String sendaddress = mBean.get("sendaddress").toString();
        if(AppUtil.getString(R.string.heder_type_company).equals(customertype)){
            ll_tax_pay.setVisibility(View.VISIBLE);
            if(AppUtil.getString(R.string.invoice_type_normal).equals(invtype)){
                ll_buy_info_c.setVisibility(View.GONE);
                view_item1.setVisibility(View.GONE);
                iv_invoice_confirm.setImageResource(R.drawable.bg_invoice_tag_normal);
            }else if(AppUtil.getString(R.string.invoice_type_special).equals(invtype)){
                ll_buy_info_c.setVisibility(View.VISIBLE);
                view_item1.setVisibility(View.VISIBLE);
                view_item2.setVisibility(View.VISIBLE);
                iv_invoice_confirm.setImageResource(R.drawable.bg_invoice_tag_special);

                String zzs_openbyzbank = mBean.get("zzs_openbyzbank").toString();
                String zzs_bankcardno = mBean.get("zzs_bankcardno").toString();
                String zzs_address = mBean.get("zzs_address").toString();
                String zzs_phone = mBean.get("zzs_phone").toString();
                String zzs_companyname = mBean.get("zzs_companyname").toString();
                tv_open_bank_c.setText(zzs_openbyzbank);
                tv_bank_account_c.setText(zzs_bankcardno);
                tv_address_c.setText(zzs_address);
                tv_telphone_c.setText(zzs_phone);
                tv_company_name_c.setText(zzs_companyname);
            }

            String zzs_taxno = mBean.get("zzs_taxno").toString();
            tv_taxpay_c.setText(zzs_taxno);

        }else if(AppUtil.getString(R.string.heder_type_person).equals((customertype))){
            iv_invoice_confirm.setImageResource(R.drawable.bg_invoice_tag_normal);
            ll_tax_pay.setVisibility(View.GONE);
            ll_buy_info_c.setVisibility(View.GONE);
            view_item1.setVisibility(View.GONE);
        }

        tv_header_type.setText(customertype);
        tv_invoice_type_c.setText(invtype);
        tv_invoice_header_c.setText(customername);
        tv_invoice_content_c.setText(invoiceitem);
        if("0".equals(taxpayertype)){
            tv_taxpayer_type_c.setText(AppUtil.getString(R.string.taxpay_normal));
        }
        tv_invoice_mount_c.setText("¥" + amt);
        tv_addressee_c.setText(addressee);
        tv_tel_num_c.setText(mobile);
        tv_receive_address_c.setText(sendaddress);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_confirm_bill:
                showConfirmNoticeDialog();
                break;
        }
    }

    public void showConfirmNoticeDialog(){

        final View mEditDialog = View.inflate(mContext, R.layout.dialog_invoice_confirm, null);
        TextView tv_cancel= (TextView) mEditDialog.findViewById(R.id.tv_cancel);
        TextView tv_confirm= (TextView) mEditDialog.findViewById(R.id.tv_confirm);
        tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
        mConfirmDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 217);
        mConfirmDialog.show();
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clientRequest(bean);
                DialogUtil.getInstance().dismiss();
            }
        });
    }


    private void clientRequest(EntityBean bean) {
        DZZWTools.showWaitDialog(this);
        EntityBean serBean = new EntityBean();
        EntityBean Bean = new EntityBean();
        EntityBean listBean = new EntityBean();
        EntityBean  invoicebean= (EntityBean) bean.get("invoicebean");
        String customertype = invoicebean.get("customertype").toString();
        String invtype = invoicebean.get("invtype").toString();
        String invoiceitem = invoicebean.get("invoiceitem").toString();
        invoicebean.set("customertype",getHeaderType(customertype));
        invoicebean.set("invtype",getInvType(invtype));
        invoicebean.set("invoiceitem",getInvoiceItem(invoiceitem));
        invoicebean.set("cardno", UserInfor.getInstance().getUsersfzh());
        serBean.set("chargelistids", invoiceIds);
        serBean.set("invoicebean", invoicebean);
        Bean.set("bean", serBean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "addInvoice", Bean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    clientParse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }



            @Override
            public void onFinished(String key, String service) {
                DZZWTools.dismissDialog(null);
                if(!TextUtils.isEmpty(Control.getRecipientName())){
                    Control.setRecipientName("");
                }

                if(!TextUtils.isEmpty(Control.getRecipientTel())){
                    Control.setRecipientTel("");
                }

                if(!TextUtils.isEmpty(Control.getRecipientAdd())){
                    Control.setRecipientAdd("");
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                DZZWTools.showToast(InvoiceConfirmActivity.this, getResources().getString(R.string.networkerror), 1000 * 2);
            }

        });
    }

    private void clientParse(Object result) throws Exception {
        EntityBean bean = (EntityBean) result;
        int state = bean.getInt("resultstate");
        String id = (String) bean.get("result");
        PrintLog.e("Invoiceconfirm","id=:"+bean.get("result"));
        if (state == 1) {
            DZZWTools.showToast(this, getString(R.string.invoice_commit_success), Toast.LENGTH_SHORT);
            Intent intent =new Intent(this,BillDetailActivity.class);
            intent.putExtra(BillDetailActivity.INVOICE_ID,id);
            intent.setFlags(INVOICE_CONFIRM_ACTIVITY);
            startActivity(intent);
        } else {
            DZZWTools.showToast(this, bean.getString("resultdesc"), Toast.LENGTH_SHORT);
        }
    }

    public int getInvType(String type){
        if((AppUtil.getString(R.string.invoice_type_normal)).equals(type)){
            invoiceType = 3;
        }else if(AppUtil.getString(R.string.invoice_type_special).equals(type)){
            invoiceType = 4;
        }
        return invoiceType;
    }

    public int getHeaderType(String type){
        if(AppUtil.getString(R.string.heder_type_person).equals(type)){
            headerType = 0;
        }else if(AppUtil.getString(R.string.heder_type_company).equals(type)){
            headerType = 1;
        }
        return headerType;
    }

    public int getInvoiceItem(String type){
        if(AppUtil.getString(R.string.invoice_content_service).equals(type)){
            invoiceItem = 4;
        }else if(AppUtil.getString(R.string.invoice_content_course).equals(type)){
            invoiceItem = 5;
        }
        return invoiceItem;
    }
}
